﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class ManageProblemType : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindMainGridView();
            }
        }

        private void BindMainGridView()
        {
            ProblemTypeService service = new ProblemTypeService();
            MainGridView.DataSource = service.GetAllProblemType();
            MainGridView.DataBind();
        }

        protected void MainGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string idStr = e.CommandArgument.ToString();
            ProblemTypeService service = new ProblemTypeService();
            switch (e.CommandName)
            {
                case "EditType":
                    IDHiddenField.Value = idStr;
                    ProblemType problemType = service.GetTypeByID(Convert.ToInt32(idStr));
                    editNameTxtBox.Text = problemType.Name;

                    EditMsgLbl.Text = "";
                    EditNameModal.Show();
                    break;
                case "DeleteType":
                    try
                    {
                        service.DeleteProblemTypeByID(Convert.ToInt32(idStr));
                        BindMainGridView();
                    }
                    catch (Exception ex)
                    {
                        string alertStr = "alert('删除失败，" + ex.Message + "');";
                        ClientScript.RegisterStartupScript(this.GetType(), "tip", alertStr, true);
                    }
                    break;
            }
        }

        protected void saveBtn_Click(object sender, EventArgs e)
        {
            // 查询是否有重名
            string newName = editNameTxtBox.Text.Trim();
            ProblemTypeService service = new ProblemTypeService();
            if (service.IsDuplicateByUpdateName(newName, Convert.ToInt32(IDHiddenField.Value)))
            {
                EditMsgLbl.Text = "该名称已存在！";
                EditNameModal.Show();
            }
            else
            {
                try
                {
                    service.UpdateName(newName, Convert.ToInt32(IDHiddenField.Value));
                }
                catch (Exception ex)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('修改失败，系统错误！');", true);
                }
                finally
                {
                    BindMainGridView();
                }
            }
        }

        protected void addBtn_Click(object sender, EventArgs e)
        {
            // 查询是否有重名
            string newName = addNameTxtBox.Text.Trim();
            ProblemTypeService service = new ProblemTypeService();
            if (service.IsDuplicateByUpdateName(newName, -1))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('该名称已存在！');", true);
            }
            else
            {
                try
                {
                    ProblemType problemType = new ProblemType();
                    problemType.Name = newName;
                    service.AddProblemType(problemType);
                }
                catch (Exception ex)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('添加失败，系统错误！');", true);
                }
                finally
                {
                    BindMainGridView();
                }
            }
        }
    }
}
